package org.example.hansp.array_.array_exercise;
/*
* 随机生成100个小写字母放到数组中，统计每个字母出现的次数*/
public class CountLetterInArray {
    public static void main(String[] args) {
        char[] chars = createArray();
        printArray(chars);
        System.out.println("==========================================");
        System.out.println("==========================================");
        int[] counts = countLetterInArray(chars);
        displayCounts(counts);

    }
    public static char[] createArray(){
        char[] chars=new char[100];
        for(int i=0;i<chars.length;i++){
            chars[i]=RandomCharacter.getRandomLowerCharacter();
        }
        return chars;
    }
    public static void printArray(char[] chars){
        for(int i=0;i<chars.length;i++){
            if((i+1)%20==0){
                System.out.println(chars[i]);
            }else {
                System.out.print(chars[i] + " ");
            }
        }
    }
    public static int[] countLetterInArray(char[] chars){
        int[] counts=new int[26];
        for(char c:chars){
            counts[c-'a']++;
        }
        return counts;
    }

    public static void displayCounts(int[] counts){
        for(int i=0;i<counts.length;i++){
            if((i+1)%10==0){
                System.out.println(counts[i]+" "+(char)(i+'a'));
            }
            System.out.printf(counts[i]+" "+(char)(i+'a')+" ");;
        }
    }

}
